import bluetooth from '@ohos.bluetooth';
import prompt from '@ohos.prompt';
class BlueTooth{
  constructor(){
  }
 public  OpenBluetooth():Boolean{
    let enable:Boolean = bluetooth.enableBluetooth()
    if(enable==true){
      prompt.showToast({
        message: 'Open bluetooth  ' + enable,
        duration: 2000,
      });
    }
   return enable
  }
  CloseBluetooth(){


  }

  ReadBluetooth( BLEDevice: any,){

    // logger.info(`connectState = ${JSON.stringify(connectState)},连接成功`)
    //mycode
    BLEDevice.getServices().then((result: Array<bluetooth.GattService>) => {
      for (let blueService of result) {
        // logger.info(TAG, `BLE Device blue Service uuid=${blueService.serviceUuid}`)
        if ('00002760-08C2-11E1-9073-0E8AC72E1001'=== blueService.serviceUuid) {
          let curCharacteristics = blueService.characteristics
          for (let characteristic of curCharacteristics) {
            // logger.info(TAG, `BLE Device characteristic= ${JSON.stringify(characteristic)}`)
            if ('00002760-08C2-11E1-9073-0E8AC72E0002' === characteristic.characteristicUuid) {
              // let mNotifyCharacteristic:bluetooth.BLECharacteristic= characteristic
              // logger.info(TAG,`${JSON.stringify(this.mNotifyCharacteristic)}`)
              function readCcc(code, BLECharacteristic) {
                if (code.code != 0) {
                  return;
                }
                // logger.info(TAG,'bluetooth characteristic uuid: ' + BLECharacteristic.characteristicUuid);
                let value = new Uint8Array(BLECharacteristic.characteristicValue);
                // logger.info(TAG,'bluetooth characteristic value: ' + value[0] +','+ value[1]+','+ value[2]+','+ value[3]+',length:'+value.length);
                // for(let f of value){
                //   logger.info(TAG,`bluetooth characteristic value:${f}`)
                // }
                if(value[0]!=9||value.length<9){
                  return;
                }
                let result:number[]=[0,0,0,0]
                result[0]=value[2]|value[1]<<8;
                result[1]=value[4]|value[3]<<8;
                result[2]=value[6]|value[5]<<8;
                result[3]=value[8]|value[7]<<8;
              }
              let descriptors = [];
              let bufferDesc = new ArrayBuffer(8);
              let descV = new Uint8Array(bufferDesc);
              descV[0] = 11;
              let descriptor = {serviceUuid: '00002760-08C2-11E1-9073-0E8AC72E1001',
                characteristicUuid: '00002760-08C2-11E1-9073-0E8AC72E0002',
                descriptorUuid: '00002902-0000-1000-8000-00805F9B34FB', descriptorValue: bufferDesc};
              descriptors[0] = descriptor;
              let bufferCCC = new ArrayBuffer(8);
              let cccV = new Uint8Array(bufferCCC);
              cccV[0] = 1;
              let characteristic1 = {serviceUuid: '00002760-08C2-11E1-9073-0E8AC72E1001',
                characteristicUuid: '00002760-08C2-11E1-9073-0E8AC72E0002',
                characteristicValue: bufferCCC, descriptors:descriptors};
              BLEDevice.readCharacteristicValue(characteristic1,readCcc);
            }
          }
        }
      }
    });

    prompt.showToast({
      message: '连接成功',
      duration: 2000,
    });
  }


}

export default new BlueTooth()